package com.moyangfenghun.javabase.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.moyangfenghun.javabase.bean.TestBean;
import com.moyangfenghun.javabase.bean.vo.SysUserDTO;
import com.moyangfenghun.javabase.bean.vo.SysUserView;
import com.moyangfenghun.javabase.common.ApiResult;
import com.moyangfenghun.javabase.model.SysUserEntity;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("test")
public class TestController {

    @Value("${spring.profiles.active}")
    public String active;

    @GetMapping("helloWorld")
    public String helloWorld() {
        return "hello world!  66"+active;
    }

    @PostMapping("downfileexcel")
    public void downfileExcel(HttpServletRequest request, HttpServletResponse response){
        String filename = "模板";
        List<TestBean> list = new ArrayList<>();
        TestBean testBean = new TestBean();
        list.add(testBean);
        ExportParams exportParams = new ExportParams(null, filename, ExcelType.XSSF);
        // 自定义字典查询规则
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, TestBean.class, list);
        if (workbook != null) {
            try {
                response.setCharacterEncoding("UTF-8");
                response.setHeader("content-Type", "application/vnd.ms-excel");
                response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8") + ".xls");
                workbook.write(response.getOutputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @GetMapping("neiwangchuangtou")
    public ApiResult<String> neiwangchuangtou(HttpServletRequest request, HttpServletResponse response){
        return new ApiResult<String>(0, "内网穿透成功!",null);
    }

    @GetMapping("view")
    public ApiResult<SysUserEntity> viewtest(HttpServletRequest request, HttpServletResponse response){
        SysUserEntity sysUser = new SysUserEntity();
        sysUser.setId(1);
        sysUser.setUsername("abc");
        sysUser.setStatus(0);
        sysUser.setPassword("123456");
        return new ApiResult<SysUserEntity>(0, "JsonView!",sysUser);
    }

    @GetMapping("viewapi")
    @JsonView(value = SysUserView.Api.class)
    public ApiResult<SysUserEntity> viewtestapi(HttpServletRequest request, HttpServletResponse response){
//        BeanUtils.copyProperties();
        SysUserEntity sysUser = new SysUserEntity();
        sysUser.setId(1);
        sysUser.setUsername("abc");
        sysUser.setStatus(0);
        sysUser.setPassword("123456");
        return new ApiResult<SysUserEntity>(0, "JsonView!",sysUser);
    }

    @GetMapping("viewall")
    @JsonView(value = SysUserView.All.class)
    public ApiResult<SysUserEntity> viewtestall(HttpServletRequest request, HttpServletResponse response){
        //    @JsonFilter("") 难用，可读性差
//    @JsonView   注解繁多，如果view比较多复杂,如果要返回全部则，似乎不支持接口文档， 一样需要复制属性值
//    @JsonIgnore 所有正/反序列化接口都生效
        // 1.总结，按需返回，不用事先设计vo，dto。需要再用，其次不复杂的情况下优先考虑@jsonview
//        BeanUtils.copyProperties();
        SysUserEntity sysUser = new SysUserEntity();
        sysUser.setId(1);
        sysUser.setUsername("abc");
        sysUser.setStatus(0);
        sysUser.setPassword("123456");
        return new ApiResult<SysUserEntity>(0, "JsonView!",sysUser);
    }

    @GetMapping("viewallp")
    @JsonView(value = SysUserView.AllP.class)
    public ApiResult<SysUserEntity> viewtestallp(HttpServletRequest request, HttpServletResponse response){
        SysUserEntity sysUser = new SysUserEntity();
        sysUser.setId(1);
        sysUser.setUsername("abc");
        sysUser.setStatus(0);
        sysUser.setPassword("123456");
        return new ApiResult<SysUserEntity>(0, "JsonView!",sysUser);
    }

    @GetMapping("viewpu")
    @JsonView(value = SysUserView.Public.class)
    public ApiResult<SysUserEntity> viewtestpu(HttpServletRequest request, HttpServletResponse response){
        SysUserEntity sysUser = new SysUserEntity();
        sysUser.setId(1);
        sysUser.setUsername("abc");
        sysUser.setStatus(0);
        sysUser.setPassword("123456");
        return new ApiResult<SysUserEntity>(0, "JsonView!",sysUser);
    }

    @GetMapping("viewinter")
    @JsonView(value = SysUserView.Internal.class)
    public ApiResult<SysUserEntity> viewtestinter(HttpServletRequest request, HttpServletResponse response){
        SysUserEntity sysUser = new SysUserEntity();
        sysUser.setId(1);
        sysUser.setUsername("abc");
        sysUser.setStatus(0);
        sysUser.setPassword("123456");
        return new ApiResult<SysUserEntity>(0, "JsonView!",sysUser);
    }

    @GetMapping("viewdto")
    @JsonView(value = {SysUserView.AllP.class,SysUserView.All.class})
    public ApiResult<SysUserDTO> viewtestdto(HttpServletRequest request, HttpServletResponse response){
        SysUserDTO sysUser = new SysUserDTO();
        sysUser.setId(1);
        sysUser.setUsername("abc");
        sysUser.setStatus(0);
        sysUser.setPassword("123456");
        sysUser.setDescription("描述");
        return new ApiResult<SysUserDTO>(0, "JsonView!",sysUser);
    }

}
